package com.goodving.guli.bt;

import android.app.DownloadManager;
import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.MimeTypeMap;
import android.widget.TextView;
import android.widget.Toast;

import com.goodving.guli.R;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.model.Keeper;
import com.goodving.guli.view.C3247b;
import com.goodving.guli.view.C3657a;

/**
 * Created by john on 2015/12/7.
 */
public class C1839j extends DimPanelFragment {
    public static final String f10264a = "DOWNLOAD_ID_REF";
    public static final String f10265b = "UPDATE_DATE_REF";
    private static final String f10266c = "ApkUpgradeFragment";
    private String f10267d;
    private String f10268e;
    private String f10269f;

    protected int inflateLayout() {
        return R.layout.apk_upgrade;
    }

    public void onCancel(DialogInterface dialogInterface) {
        super.onCancel(dialogInterface);
        GuliLog.m12370d(f10266c, "onCancel();");
    }

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
    }

    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        View onCreateView = super.onCreateView(layoutInflater, viewGroup, bundle);
        Bundle arguments = getArguments();
        this.f10267d = arguments.getString(Utils.f3500h);
        this.f10268e = arguments.getString(Utils.f3501i);
        this.f10269f = arguments.getString(Utils.f3499g);
        ((TextView) onCreateView.findViewById(R.id.update_apk_content)).setText(this.f10269f);
        return onCreateView;
    }

    protected void onEmptyAreaClicked() {
        super.onEmptyAreaClicked();
        GuliLog.m12370d(f10266c, "onEmptyAreaClicked()");
    }

    protected void onLeftButtonClicked() {
        dismiss();
    }

    protected void onRightButtonClicked() {
        C3247b.m22928a(getActivity(), (int) R.string.start_download, 1).show();
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(this.f10268e));
        request.setAllowedNetworkTypes(3);
        request.setAllowedOverRoaming(false);
        request.setMimeType(MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(this.f10268e)));
        request.setShowRunningNotification(true);
        request.setVisibleInDownloadsUi(true);
        String str = getString(R.string.app_name) + ".apk";
        try {
            if (Environment.getExternalStorageState().equals("mounted")) {
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, str);
                DownloadManager downloadManager = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
                try {
                    Keeper.keepDownloadApkId(downloadManager.enqueue(request));
                } catch (Exception e) {
                    String str2 = Environment.getExternalStorageDirectory().getPath() + "/" + str;
                    try {
                        request.setDestinationUri(Uri.parse("file://" + str2));
                        Keeper.keepDownloadApkId(downloadManager.enqueue(request));
                    } catch (Exception e2) {
                        GuliLog.m12370d(f10266c, "upgrade apk failed : path = " + str2 + ", error = " + e2.toString());
                    }
                }
                dismiss();
                return;
            }
            Toast.makeText(getActivity(), R.string.sdcarderror, Toast.LENGTH_SHORT).show();
        } catch (Exception e22) {
            Exception exception = e22;
            GuliLog.m12370d(f10266c, new StringBuilder().append("setDestinationInExternalPublicDir = ").append(exception).toString() != null ? exception.toString() : C3657a.f18328f);
            exception.printStackTrace();
        }
    }
}
